package widget;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by LLAP on 2017/12/11.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class DirectionSelector extends JPanel implements ActionListener {
    private JButton[] directions;
    private String tooltips[] = {"NW", "N", "NE", "W", "C", "E", "SW", "S", "SE"};
    private JButton selectedButton;
    private String selected;
    public DirectionSelector(boolean center) {
        super();
        this.setSize(30, 30);
        this.setLayout(new GridLayout(3, 3, 4, 4));

        directions = new JButton[9];
        for (int i = 0; i < 9; i++) {
            directions[i] = new JButton("");
            directions[i].setToolTipText(tooltips[i]);
            directions[i].addActionListener(this);

            if (i == 4) {
                if (center) this.add(directions[i]);
                else this.add(new JLabel());
            }
            else this.add(directions[i]);
        }
    }
    public DirectionSelector() {
        this(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        selectedButton = (JButton)e.getSource();
        for (int i = 0; i < 9; i++) {
            directions[i].setText("");
        }
        selectedButton.setText(selectedButton.getToolTipText());
        selected = selectedButton.getToolTipText();
//        this.updateUI();
    }

    public String getSelectedDirection() {
        return selected;
    }

    public void clearSelection() {
        selected = "";
        selectedButton = null;
        for (int i = 0; i < 9; i++) {
            directions[i].setText("");
        }
        this.updateUI();
    }
}